#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
sys.path.append('src')

from processors.docx_processor import DOCXProcessor
from core.chunking_engine import ChunkingEngine
from core.text_cleaner import TextCleaner

def debug_choice_chunking():
    """调试选择题切片问题"""
    
    print("=== 调试选择题切片问题 ===")
    
    file_path = 'data/原始数据---选择题.docx'
    
    if not os.path.exists(file_path):
        print(f"文件不存在: {file_path}")
        return
    
    try:
        # 提取文档内容
        docx_processor = DOCXProcessor()
        with open(file_path, 'rb') as f:
            file_content = f.read()
        
        extraction_result = docx_processor.extract_content(file_content)
        raw_text = extraction_result['text']
        
        print(f"原始文本长度: {len(raw_text)} 字符")
        print(f"\n原始文本前500字符:")
        print(repr(raw_text[:500]))
        
        # 测试不同的清洗配置
        cleaning_configs = [
            {
                'name': '最小清洗',
                'config': {
                    'clean_unicode': False,
                    'remove_special_chars': False,
                    'fix_line_breaks': False,
                    'remove_headers_footers': False,
                    'remove_duplicates': False,
                    'remove_empty_lines': False,
                    'min_line_length': 0,
                    'preserve_formatting': True
                }
            },
            {
                'name': '标准清洗',
                'config': {
                    'clean_unicode': True,
                    'remove_special_chars': False,
                    'fix_line_breaks': True,
                    'remove_headers_footers': False,
                    'remove_duplicates': False,
                    'remove_empty_lines': True,
                    'min_line_length': 1,
                    'preserve_formatting': True
                }
            }
        ]
        
        chunking_config = {
            'strategy': '题目结构分段',
            'question_pattern': r'^\d+\.',
        }
        
        for clean_info in cleaning_configs:
            print(f"\n=== 测试 {clean_info['name']} ===")
            
            # 文本清洗
            text_cleaner = TextCleaner(clean_info['config'])
            cleaned_text = text_cleaner.clean_text(raw_text)
            
            print(f"清洗后文本长度: {len(cleaned_text)} 字符")
            print(f"清洗后文本前500字符:")
            print(repr(cleaned_text[:500]))
            
            # 检查题目模式
            import re
            pattern = re.compile(r'^\d+\.', re.MULTILINE)
            matches = pattern.findall(cleaned_text)
            print(f"找到的题目模式: {len(matches)} 个")
            print(f"前10个匹配: {matches[:10]}")
            
            # 文档切片
            chunking_engine = ChunkingEngine(chunking_config)
            chunks = chunking_engine.chunk_document(cleaned_text)
            
            print(f"生成切片数: {len(chunks)} 个")
            
            if chunks:
                question_chunks = [c for c in chunks if c['metadata'].get('chunk_type') == 'question']
                print(f"题目切片数: {len(question_chunks)} 个")
                
                # 显示前3个切片
                for i, chunk in enumerate(chunks[:3]):
                    print(f"\n切片 {i+1}:")
                    print(f"  类型: {chunk['metadata'].get('chunk_type', 'unknown')}")
                    print(f"  题号: {chunk['metadata'].get('question_number', 'N/A')}")
                    print(f"  长度: {chunk['metadata']['char_count']} 字符")
                    content_preview = chunk['content'][:100].replace('\n', ' ')
                    print(f"  内容: {content_preview}...")
        
        # 手动分析文本结构
        print(f"\n=== 手动分析文本结构 ===")
        lines = raw_text.split('\n')
        print(f"总行数: {len(lines)}")
        
        # 查找题目行
        question_lines = []
        for i, line in enumerate(lines):
            line_stripped = line.strip()
            if re.match(r'^\d+\.', line_stripped):
                question_lines.append((i, line_stripped))
        
        print(f"找到题目行: {len(question_lines)} 个")
        for i, (line_num, content) in enumerate(question_lines[:10]):
            print(f"  行 {line_num}: {content[:50]}...")
        
    except Exception as e:
        print(f"调试过程中出错: {e}")
        import traceback
        traceback.print_exc()

if __name__ == '__main__':
    debug_choice_chunking()